$(function () {
	document.getElementById("password").onkeyup = function () {
        delPwdStr();
        var pwd = this.value;
        if (pwd.length < 6 || pwd.length > 20) {
            showInfo("请输入6-20位的,字母和数字",$s("password"))
        } else {
            var lv = 0;
            if (pwd.match(/[a-z]/g)) {lv++;}
            if (pwd.match(/[A-Z]/g)) {lv++;}
            if (pwd.match(/[0-9]/g)) {lv++;}
            if (pwd.length > 10) {lv++}
            if (pwd.length > 13) {lv++}
            if (pwd.length > 16) {lv++}
            if (pwd.length > 18) {lv++}
            lv = Math.ceil(lv * 3 / 7);
            lv = (lv > 3) ? 3 : lv;
            if (lv === 1) {
                showPwdSrt("images/ruo.png");
                showOk("弱", $s("password"))
            } else if (lv === 2) {
                showPwdSrt("images/zhong.png");
                showOk("中", $s("password"))
            } else {
                showPwdSrt("images/qiang.png");
                showOk("强", $s("password"))
            }
        }
    }
})

$(function(){
	 $("input[type=text],textarea").focus(function(){
		  $(this).addClass("input_focus");
		}).blur(function(){
				$(this).removeClass("input_focus");
		});

	$(".form_btn").hover(function(){
			$(this).css("color","red").css("background","#6FB2DB");
		},
		
		function(){
			$(this).css("color","#295568").css("background","#BAD9E3");
		});	
	
});	

function showPwdSrt(img) {
    var id = document.getElementById("pwddiv");
    var div = document.createElement('div');
    var oStrength = id.appendChild(div);
    oStrength.className = "passStrength";
    oStrength.style.backgroundImage = "url(" + img + ")";
}

function delPwdStr() {
    $("#pwddiv div").remove();
}

function showInfo(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "#ff9800";
    document.getElementById(labelId).innerHTML = msg;
}

function showError(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "red";
    document.getElementById(labelId).innerHTML = msg;
    return 1;
}

function showInfoly(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "#8FA0AC";
    document.getElementById(labelId).innerHTML = msg;
}

function showOk(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "green";
    document.getElementById(labelId).innerHTML = msg;
}

function $s(id) {
    return document.getElementById(id);
}

function validateUserName(item) {
    var text = item.value;
    var reg = /^[0-9a-zA-Z]{6,20}$/;
    if (text === "") {
        showError("用户名不能为空", item);
    } else if (reg.test(text) == false) {
        showError("格式错误,请检查后重新输入", item);
    } else if (text.length < 6 || text.length > 20) {
        showError("用户名长度应在6-20之间,请检查后输入", item);
    } else {
    $.ajax({
		 url:"UserServlet",
		 type:"POST",
		 async:false,  //在进行验证的时候,这里最好不用异步
		 data:{flag:"uname",adminName:text},
		 success:function(data){
			 if(data=="1"){
				 showError("用户名已经被使用", item);
			 }else if (data=="2") {
				 showError("用户名不能为空", item);
			}else {
				 showOk("用户名验证通过", item)
			}
		 } 
	 }); 
    }
}

function validatePassword(item) {
    var pwd = item.value;
    var reg = /^[0-9a-zA-Z]{6,20}$/;
    if (pwd === "") {
        showError("密码不能为空", item);
    } else if (reg.test(pwd) === false) {
        showError("格式错误,请检查后重新输入", item);
    } else if (pwd.length < 6 || pwd.length > 20) {
        showError("密码长度应在6-20之间,请检查后输入", item);
    } else {

    }
}

function verifyPasswordRepeatedly(item) {
    var text = item.value;
    var vpwd = document.getElementById("password").value;
    if (text === "") {
        showError("重复密码不能为空", item);
    } else if (text === vpwd) {
        showOk("重复密码验证通过", item)
    } else {
        showError("重复密码错误", item);
    }

}

function validate() {
    var result = 0;

    if ($s("adminName").value === "") {
        result = result + showError("用户名不能为空", $s("adminName"));
    } else if ($('#adminName_msg').css('color') === "red") {
        result = result + 1;
    }

    if ($s("password").value === "") {
        result = result + showError("密码不能为空", $s("password"));
    } else if ($('#password_msg').css('color') === "red") {
        result = result + 1;
    }

    if ($s("pwdconfirm").value === "") {
        result = result + showError("重复密码不能为空", $s("pwdconfirm"));
    } else if ($('#pwdconfirm_msg').css('color') === "red") {
        result = result + 1;
    }
    if (result === 0) {
        result = confirm("确定要提交吗");
        return result;
    } else {
        return false;
    }
}
